var express = require('express');
var localStorage = require('node-localstorage')
var router = express.Router();
var dbcomm = require('../util/dbcomm');
var db = require('../util/db');
var moment = require('moment');
var multer  = require('multer');//用express的第三方中间件 multer 实现文件上传功能。
var now = moment().format('YYYY年MM月DD日') ;//日期
const bodyParser = require('body-parser');
var upload = multer({dest:'./templates/BackStage/image'});


//处理页面数据 
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());

router.get('/logout',function(req,res){
    console.log(req.session);
    //打印session的值
     req.session.destroy(function(err) {
            if(err){
                console.log('退出登录失败');
                return;
            }
            res.clearCookie('hzw');
            res.redirect('/');
    });
});

router.post('/mysession',function(req,res){
    res.send(req.session);
});


router.post('/',function(req,res){
    var datas = req.body;
    var sql = 'SELECT * FROM sys_login WHERE staffNo =? and staffPass =?';
    dbcomm.query(sql,[datas.staffNo,datas.staffPass],function(err,data){
        if(err) throw err;
        else{
            if(1 == data.length){
                //代表找到了用户信息
                if (req.session.sign) {
                    //检查用户是否已经登录
                    res.send({Result:'ok'});
                } else {
                    //否则展示index页面
                    req.session.sign = true;
                    req.session.staffNo = data[0].staffNo;
                    req.session.staffName = data[0].staffName;
                    req.session.staffPass = data[0].staffPass;
                    req.session.staffRole = data[0].staffRole;
                    res.send({Result:'ok'});
                }
            }else if(0 == data.length){
                res.send({Result:'no'});
            }
        }
    });
   
});

router.post('/selectLogin',function(req,res){
    var para = req.body;
    var sql = '';
    sql = "SELECT * FROM sys_login ";
    db.page(sql,para,function(data){
        res.send(data);
    });
});

router.post('/deleteLogin',function(req,res){
    var para = req.body;
    var sql='';
    sql ="delete from sys_login where "+para.primaryKey+" = '"+para.primaryKeyName+"'";
    db.delete(sql,para,function(data){
        res.send(data);
    });
});

router.post('/insertLogin',upload.array('IMAGE',9),function(req,res){
    var para = req.body;
    var arr = req.query;
    var file=req.files;
    if(file){
        file.forEach(function(items,i){
            console.log("name["+i+"]:"+arr.fileName[i]);
            name=items.originalname;
            nameArray=name.split('');
            var nameMime=[];
            l=nameArray.pop();
            nameMime.unshift(l);
            while(nameArray.length!=0&&l!='.'){
                l=nameArray.pop();
                nameMime.unshift(l);
            }
            Mime=nameMime.join('');
            fs.renameSync('./templates/BackStage/image/'+items.filename,'./templates/BackStage/image/'+items.filename+Mime);
            para[arr.fileName[i]] = '/BackStage/image/'+items.filename+Mime;
            console.log(arr.fileName[i])
        })
    }
    var sql='';
    sql ="INSERT INTO sys_login";
    db.insert(sql,para,function(data){
        if(data.result == 'succeed'){
            res.redirect(para.returnUrl);
        }
    });
});

router.post('/updateLogin',upload.array('IMAGE',9),function(req,res){
    var para = req.body;
    console.log(para);
    var arr = req.query;
    var file=req.files;
    if(file){
        file.forEach(function(items,i){
            console.log("name["+i+"]:"+arr.fileName[i]);
            name=items.originalname;
            nameArray=name.split('');
            var nameMime=[];
            l=nameArray.pop();
            nameMime.unshift(l);
            while(nameArray.length!=0&&l!='.'){
                l=nameArray.pop();
                nameMime.unshift(l);
            }
            Mime=nameMime.join('');
            fs.renameSync('./templates/BackStage/image/'+items.filename,'./templates/BackStage/image/'+items.filename+Mime);
            para[arr.fileName[i]] = '/BackStage/image/'+items.filename+Mime;
        })
    }
    var sql='';
    sql ="update sys_login SET ";
    db.update(sql,para,function(data){
        if(data.result == 'succeed'){
            res.redirect(para.returnUrl);
        }
    });
});

module.exports=router;